package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.externalcontact.moment.GetMomentListVo;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "获取企业全部的发表列表", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "start_time", type = Type.Integer, description = "朋友圈记录开始时间。Unix时间戳", required = true),
        @Parameter(name = "end_time", type = Type.Integer, description = "朋友圈记录结束时间。Unix时间戳", required = true),
        @Parameter(name = "creator", type = Type.String, description = "朋友圈创建人的userid", required = false),
        @Parameter(name = "filter_type", type = Type.Integer, description = "朋友圈类型。0：企业发表 1：个人发表 2：所有，包括个人创建以及企业创建，默认情况下为所有类型", required = false),
        @Parameter(name = "cursor", type = Type.String, description = "用于分页查询的游标，字符串类型，由上一次调用返回，首次调用可不填", required = false),
        @Parameter(name = "limit", type = Type.Integer, description = "返回的最大记录数，整型，最大值100，默认值100，超过最大值时取默认值", required = false),
})
@Component
public class GetMomentList implements Action {

    @Autowired
    private ExternalContactService externalContactService;


    @Override
    public Response execute(Request request) throws Exception {
        Integer start_time = request.getInteger("start_time");
        Integer end_time = request.getInteger("end_time");
        String creator = request.getString("creator");
        Integer filter_type = request.getInteger("filter_type");
        String cursor = request.getString("cursor");
        Integer limit = request.getInteger("limit");

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("start_time", start_time);
        params.put("end_time", end_time);

        if (creator != null) {
            params.put("creator", creator);
        }
        if (filter_type != null) {
            params.put("filter_type", filter_type);
        }
        if (cursor != null) {
            params.put("cursor", cursor);
        }
        if (limit != null) {
            params.put("limit", limit);
        }

        GetMomentListVo rsp = externalContactService.getMomentList(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
